<template>
  <view class="category">
    <ugo-search></ugo-search>
    <view class="main">
      <view class="menus">
        <view :class="['item', index === current ? 'active' : '']" v-for="(item, index) in menus" :key="item" @click="handleClick(index)">{{
          item
        }}</view>
      </view>
      <scroll-view class="goods" :scroll-top="scrollTop" scroll-y>
        <view class="goods-group" v-for="item1 in goodsList" :key="item1.cat_id">
          <view class="goods-group-title">{{ item1.cat_name }}</view>
          <view class="goods-group-content">
            <navigator
              :url="'/pages/goods_list/goods_list?cid=' + item2.cat_id"
              class="goods-item"
              v-for="item2 in item1.children"
              :key="item2.cat_id"
            >
              <image :src="item2.cat_icon"></image>
              <view class="goods-name">{{ item2.cat_name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 接口的返回数据 完整！！！
        categoryList: [],

        // 左侧要渲染的标题数组
        menus: [],

        // 右侧要显示的内容数据
        goodsList: [],
        // 选中标题的索引
        current: 0,
        // 控制右侧 scroll-view标签的滚动距离
        scrollTop: 0
      }
    },
    async onLoad() {
      // 先判断一下有没有缓存
      const category = uni.getStorageSync('category')

      if (!category) {
        // 没有缓存  发送请求获取数据
        console.log('没缓存 发送请求')
        this.getCategory()
      } else {
        // 有缓存 过期时间10s
        console.log('有缓存 准备判断过期时间')
        if (Date.now() - category.time > 1000 * 10) {
          // 过期了
          console.log('缓存过期了 重新发送请求')
          this.getCategory()
        } else {
          console.log('缓存没有过期 准备拿缓存的数据来使用')
          this.setCategory(category.data)
        }
      }
    },
    methods: {
      // 发送请求获取数据
      async getCategory() {
        const { message } = await uni.$u.http.get('/categories')
        this.setCategory(message)
      },
      // 设置页面需要用到的数据
      async setCategory(message) {
        this.categoryList = message
        // 构造左侧标题数组
        this.menus = this.categoryList.map(item => item.cat_name)
        // 构造右侧要显示的商品数据
        this.goodsList = this.categoryList[this.current].children
        // 把数据存入到本地存储
        uni.setStorageSync('category', {
          data: this.categoryList,
          time: Date.now()
        })
      },
      // 左侧标题的点击
      handleClick(index) {
        this.current = index
        this.goodsList = this.categoryList[index].children

        // 设置右侧滚动容器的距离
        // 每次点击的时候都生成一个无限接近于0的不重复的值 OK
        let scrollTop = Math.random() * Math.random() * Math.random() * Math.random() * Math.random() * Math.random()

        if (this.scrollTop === scrollTop) {
          scrollTop = scrollTop / 10
        }
        this.scrollTop = scrollTop
      }
    }
  }
</script>

<style lang="scss">
.category {
  /* 小程序中 100vh 自动帮我们去除掉 tabbar的高度！！ 也去除掉顶部的高度  */
  height: 100vh;
  display: flex;
  flex-direction: column;
  .main {
    flex: 1;
    display: flex;
    overflow: hidden;
    .menus {
      width: 182rpx;
      overflow: auto;
      .item {
        height: 80rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
        color: #666;
      }
      .active {
        color: #eb4450;
        border-left: 1rpx solid #eb4450;
      }
    }
    .goods {
      flex: 1;
      overflow: auto;
    }
  }
}

.goods {
  .goods-group {
    .goods-group-title {
      height: 80rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #666;
      font-size: 28rpx;
      &::before {
        content: '/';
        margin-right: 30rpx;
        color: #ccc;
      }
      &::after {
        color: #ccc;
        content: '/';
        margin-left: 30rpx;
      }
    }

    .goods-group-content {
      display: flex;
      flex-wrap: wrap;
      .goods-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 33.33%;
        image {
          width: 120rpx;
          height: 120rpx;
          background-color: #ccc;
        }
        .goods-name {
          color: #666;
          font-size: 28rpx;
        }
      }
    }
  }
}
</style>